
package com.limoilux.cobolux.ui.wizards;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.limoilux.cobolux.core.CoboluxCoreUtils;

class ProjectWizardPage extends RessourceWizardPage
{
	private Text projectName = null;

	public ProjectWizardPage(ISelection selection)
	{
		super("toto");

		this.setTitle("Limoilux Cobol Project");
		this.setDescription("This wizard creates a Cobol project.");

	}

	private void dialogChanged()
	{
		IWorkspaceRoot root = null;
		IProject project = null;
		String projectName = null;
		String status = null;

		projectName = this.getProjectName();
		root = CoboluxCoreUtils.getWorkspaceRoot();

		if (projectName.length() <= 0)
		{
			status = "Project name must be specified";
		}
		else
		{
			project = root.getProject(projectName);
			if (project.exists())
			{
				status = "Project name is unavailible";
			}

		}

		this.updateStatus(status);
	}

	private void updateStatus(String message)
	{
		this.setErrorMessage(message);
		this.setPageComplete(message == null);
	}

	public String getProjectName()
	{
		return this.projectName.getText();
	}

	@Override
	public void createControl(Composite parent)
	{
		Composite container = null;
		GridLayout layout = null;
		GridData gridData = null;
		Label label = null;

		container = new Composite(parent, SWT.NULL);
		layout = new GridLayout();

		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;

		label = new Label(container, SWT.NULL);
		label.setText("&Project name:");

		this.projectName = new Text(container, SWT.BORDER | SWT.SINGLE);
		gridData = new GridData(GridData.FILL_HORIZONTAL);

		this.projectName.setLayoutData(gridData);
		this.projectName.addModifyListener(new ModListener());

		this.dialogChanged();
		this.setControl(container);
	}

	private class ModListener implements ModifyListener
	{
		@Override
		public void modifyText(ModifyEvent e)
		{
			dialogChanged();
		}
	}
}
